using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Gizmox.WebGUI.Forms;
using System.Threading;
using System.IO;
using System.Xml;
using VisualDB.Entities;
using System.Text.RegularExpressions;
using System.Web.Configuration;

namespace VisualDB.Helpers
{
    public class ConfigurationHelper
    {
        private static ConfigurationHelper _instance;

        public static ConfigurationHelper Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ConfigurationHelper();

                return _instance;
            }
        }

        public ConfigurationHelper()
        {
        }

        public List<DatabaseEntity> GetRegisteredDatabases()
        {
            ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;

            List<DatabaseEntity> databases = new List<DatabaseEntity>();

            foreach (ConnectionStringSettings connection in connections)
            {
                DatabaseEntity database = new DatabaseEntity();

                switch (connection.ProviderName)
                {
                    case "System.Data.SqlClient":
                        Match m1 = Regex.Match(connection.ConnectionString, @"Data Source=(?<ServerName>.+);Initial Catalog=(?<DatabaseName>\w+);User Id=(?<Username>\w*);Password=(?<Password>[\w.]*);Integrated Security=(?<IntegratedSecurity>\w+);");
                        database.DatabaseType = DatabaseType.GetDatabaseType(DatabaseType.SQLServer);
                        database.ServerName = m1.Groups["ServerName"].Value;
                        database.DatabaseName = m1.Groups["DatabaseName"].Value;
                        database.UseWindowsAuthentication = bool.Parse(m1.Groups["IntegratedSecurity"].Value);
                        database.Username = m1.Groups["Username"].Value;
                        database.Password = m1.Groups["Password"].Value;
                        break;

                    case "MySql.Data.MySqlClient":
                        Match m2 = Regex.Match(connection.ConnectionString, @"Server=(?<ServerName>.+);Database=(?<DatabaseName>\w+);Username=(?<Username>\w*);Password=(?<Password>[\w.]*);");
                        database.DatabaseType = DatabaseType.GetDatabaseType(DatabaseType.MySQL);
                        database.ServerName = m2.Groups["ServerName"].Value;
                        database.DatabaseName = m2.Groups["DatabaseName"].Value;
                        database.Username = m2.Groups["Username"].Value;
                        database.Password = m2.Groups["Password"].Value;
                        break;
                }

                databases.Add(database);
            }

            return databases;
        }

        public void AddRegisteredDatabase(DatabaseEntity database)
        {
            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

            ConnectionStringSettings connection = new ConnectionStringSettings();
            connection.Name = database.Name;

            switch (database.DatabaseType.Name)
            {
                case DatabaseType.SQLServer:
                    connection.ProviderName = "System.Data.SqlClient";
                    connection.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};Integrated Security={4};", database.ServerName, database.DatabaseName, database.Username, database.Password, database.UseWindowsAuthentication);
                    break;

                case DatabaseType.MySQL:
                    connection.ProviderName = "MySql.Data.MySqlClient";
                    connection.ConnectionString = string.Format("Server={0};Database={1};Username={2};Password={3};", database.ServerName, database.DatabaseName, database.Username, database.Password);
                    break;
            }

            configuration.ConnectionStrings.ConnectionStrings.Add(connection);
            configuration.Save();
        }

        public void UpdateRegisteredDatabase(DatabaseEntity database)
        {
            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");

            ConnectionStringSettings connection = configuration.ConnectionStrings.ConnectionStrings[database.Name];
            connection.Name = database.Name;

            switch (database.DatabaseType.Name)
            {
                case DatabaseType.SQLServer:
                    connection.ProviderName = "System.Data.SqlClient";
                    connection.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};Integrated Security={4};", database.ServerName, database.DatabaseName, database.Username, database.Password, database.UseWindowsAuthentication);
                    break;

                case DatabaseType.MySQL:
                    connection.ProviderName = "MySql.Data.MySqlClient";
                    connection.ConnectionString = string.Format("Server={0};Database={1};Username={2};Password={3};", database.ServerName, database.DatabaseName, database.Username, database.Password);
                    break;
            }

            configuration.Save();
        }

        public void DeleteRegisteredDatabase(DatabaseEntity database)
        {
            Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
            configuration.ConnectionStrings.ConnectionStrings.Remove(database.Name);
            configuration.Save();
        }
    }
}
